window._HTTP = {

    _URLPRE:"https://dragonli.cn/xyjapi",

    showMessage:function(msg){
        //console.log(msg);
        _TIPS.showMessage(msg,0);
    },

    _WXOPENID:'/wx/getOpenid?code=',

	_DYOPENID:'/dy/getOpenid?code=',

	_GETJFURL:'/cd/getJf?userId=',

	_GETCODEURL:'/cd/BuyCode?userId=',

	_CUTJFURL:'/cd/cutJf?userId=',

	_UPLOADDATAURL:'/sd/savedata?userId=',

    _UPLOADDATAURL2:'/sd/savedataForJson',

	_GETDATAURL:'/sd/getdata?userId=',

	_ISMGC:'/mgc/isMgc?mgc=',

	isMgc:function(mgc,game){
		//访问地址
		let url = this._URLPRE+this._ISMGC+mgc;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);
				if(response.code==200&&response.data==0){
					//_CDKEYMANAGER.getReward();
					game.realRename(mgc);
				}else{
					_HTTP.showMessage('改名失败,名称中包含敏感词！');
				}
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			if(_GAME){
				_GAME.showMessage('网络异常');
			}
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("GET", url, true);
		//发送请求
		xhr.send();
	},

	//获取抖音openid
	getDyOpenid:function(code,sence){

        let url = this._URLPRE+this._DYOPENID+code;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);
				if(response.code==200&&response.data&&response.data.data){
                    if(response.data.data.openid){
                        _HQM._USERNAME = response.data.data.openid;
                        _HTTP.showMessage("登录成功!");
						if(sence=='saveData'){
                        	_MSD.refreshUiCommon();
						}else if(sence=="bycode"){
							//_CHESSJFUI.refreshHome();
							_HTTP.getJf();
						}else{
							_HTTP.getJf();
						}
                    }else{
                        _TIPS.showMessage('登录异常');
                    }
				}else{
					_HTTP.showMessage(response.msg);
				}
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("GET", url, true);
		//发送请求
		xhr.send();

    },


	//获取微信openid
    getWxOpenid:function(code,sence){

        let url = this._URLPRE+this._WXOPENID+code;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);
				if(response.code==200&&response.data){
                    if(response.data.openid){
                        _HQM._USERNAME = response.data.openid;
                        _HTTP.showMessage("登录成功!");
						if(sence=='saveData'){
                        	_MSD.refreshUiCommon();
						}
						/*else if(sence=="bycode"){
							//_CHESSJFUI.refreshHome();
							_HTTP.getJf();
						}else{
							_HTTP.getJf();
						}*/
						_HTTP.getJf();
                    }else{
                        _TIPS.showMessage('登录异常');
                    }
				}else{
					_HTTP.showMessage(response.msg);
				}
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("GET", url, true);
		//发送请求
		xhr.send();

    },


	//积分兑换
	buyCode:function(code){
		var userId = _HQM._USERNAME;
		//访问地址
		let url = this._URLPRE+this._GETCODEURL+userId+'&code='+code;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);
				if(response.code==200&&response.data){
					if(response.data.stone){//神锻石兑换
						var stonenmb = response.data.stone;
						var stoneObj = _IOF.createObjByName('神锻石',null);
						stoneObj.nmb = stonenmb;
						_GAMEDATAHELPER.pushItem(stoneObj);
						_HTTP.showMessage("兑换成功!获得神锻石"+stonenmb+"(请及时存档,以免道具丢失!!!)");
					}else{
						_HTTP.showMessage("兑换成功!");
						_HTTP.getJf();
					}
				}else{
					_HTTP.showMessage(response.msg);
				}
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("POST", url, true);
		//发送请求
		xhr.send();

	},

	//消耗积分
	cutJf:function(nmb,config){

		var userId = _HQM._USERNAME;
		//访问地址
		let url = this._URLPRE+this._CUTJFURL+userId+'&nmb='+nmb+'&remark='+config.name;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);
				if(response.code==200&&response.data){

					_HTTP.afterCutJf(config);

                    _HTTP.getJf();

				}else{
					_HTTP.showMessage(response.msg);
				}
				
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("POST", url, true);
		//发送请求
		xhr.send();

	},


	afterCutJf:function(config){
		//_CHESSJFUI.refreshRewards(config);
		_CHESSJFUI.getRewardOfConfig(config);
	},


	refreshJfLabel:function(isvip,jfnmb){
		_CHESSJFUI.refreshJFInfo(isvip,jfnmb);
	},

	//获取积分
	getJf:function(){

		console.log('获取积分');
		
		var userId = _HQM._USERNAME;
		//访问地址
		let url = this._URLPRE+this._GETJFURL+userId;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				response = JSON.parse(response);
				console.log(response);

				var isvip = null;
				var jfnmb = null;

				if(response.code==200){
					if(response.data){
						isvip=response.data.isvip;
						jfnmb = response.data.jf;

						if(isvip){
							_CHESSJFUI._ISVIP = true;
						}else{
							_CHESSJFUI._ISVIP = false;
						}

						console.log('获取积分',_CHESSJFUI._ISVIP);

						_HTTP.refreshJfLabel(isvip,jfnmb);

						if(window._RANDOM){
							window._RANDOM.refreshButtons();
						}

					}else{
						_HTTP.refreshJfLabel(false,0);
					}
				}else{
					if(response.code!=200){
						_HTTP.showMessage(response.msg);
					}
				}
			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("GET", url, true);
		//发送请求
		xhr.send();

	},
	
	//上传存档
	uploadData:function(){

		var userId = _HQM._USERNAME;

        let url = this._URLPRE+this._UPLOADDATAURL+userId;

        if(
			cc.sys.platform === cc.sys.WECHAT_GAME
			||cc.sys.platform === cc.sys.BYTEDANCE_GAME
		){

            wx.request({
                url: this._URLPRE+this._UPLOADDATAURL2,
                data: {
                    userId:userId,
                    savedata: _MSD.getSaveDataStr(1),
                    savedata1:_MSD.getSaveDataStr(2),
                    savedata2:_MSD.getSaveDataStr(3),
                    savedata3:_MSD.getSaveDataStr(4),
                    savedata4:_MSD.getSaveDataStr(5)
                },
                method:'post',
                header: {
                    'Content-Type': 'application/json'
                },
                success: function(res) {
                    _HTTP.showMessage('上传成功');
                }
              })

        }else{

            var fd = new FormData()
            fd.append('savedata', _MSD.getSaveDataStr(1));
            fd.append('savedata1', _MSD.getSaveDataStr(2));
            fd.append('savedata2', _MSD.getSaveDataStr(3));
            fd.append('savedata3', _MSD.getSaveDataStr(4));
            fd.append('savedata4', _MSD.getSaveDataStr(5));

            let xhr = new XMLHttpRequest();

            xhr.timeout = 3000
            //接收数据
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
                    var response = xhr.responseText;
                    console.log(response);
                    
                    if(response){
                        response = JSON.parse(response);
                        if(response.code==200){
                            _HTTP.showMessage('上传成功');
                        }else{
                            _HTTP.showMessage(response.msg);
                        }
                    }else{
                        _HTTP.showMessage('请求异常');
                    }
                }
            };
            //错误处理
            xhr.onerror = function(evt){
                console.log(evt);
                _HTTP.showMessage('网络异常');
            }
            //初始化一个请求，GET方式，true异步请求
            xhr.open("POST", url, true);
            //发送请求
            xhr.send(fd);
        }
	},

	//下载存档
	getData:function(){
		
		
		//访问地址
		let url = this._URLPRE+this._GETDATAURL+_HQM._USERNAME;
		//新建Http
		let xhr = new XMLHttpRequest();
		//接收数据
		xhr.onreadystatechange = function () {
			if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
				var response = xhr.responseText;
				
				if(response){
					response = JSON.parse(response);
					console.log(response);
					if(response.code==200&&response.data){

						_MSD.saveDataStatic(1,response.data.savedata);
						_MSD.saveDataStatic(2,response.data.savedata1);
						_MSD.saveDataStatic(3,response.data.savedata2);
						_MSD.saveDataStatic(4,response.data.savedata3);
						_MSD.saveDataStatic(5,response.data.savedata4);

                        _HTTP.showMessage('下载成功');

                        _MSD.refreshUiCommon();
					}else{
                        _HTTP.showMessage(response.msg);
					}
				}else{
					console.log('存档',response);
					_HTTP.showMessage('请求异常');
				}

			}
		};
		//错误处理
		xhr.onerror = function(evt){
			console.log(evt);
			_HTTP.showMessage('网络异常');
		}
		//初始化一个请求，GET方式，true异步请求
		xhr.open("GET", url, true);
		//发送请求
		xhr.send();
		
		
	}

}